package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Description:
 * @Author: your name
 * @Date: 2022/6/27 0:28
 */
@Configuration
public class CorsConfig {
@Bean
    public CorsWebFilter corsWebFilter(){
        // cors配置类
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许跨域访问的域名。*-代表所有域名
        corsConfiguration.addAllowedOrigin("http://manager.gmall.com");
        corsConfiguration.addAllowedOrigin("http://localhost:1000");
        // 允许跨域访问的请求方式。*-代表所有请求方式
        corsConfiguration.addAllowedMethod("*");
        // 允许携带任何头信息
        corsConfiguration.addAllowedHeader("*");
        // 是否允许携带cookie。域名不能为*
        corsConfiguration.setAllowCredentials(true);

        // 配置源
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**", corsConfiguration);
        // 初始化一个过滤器
        return new CorsWebFilter(configurationSource);
    }

}
